using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy2 : Enemy
{
    public Transform shootpos;
    public float lastAttckTime;
    public float AttckCd = 2;
    void Start()
    {
        lastAttckTime = Time.time - AttckCd;
        anim = GetComponent<Animator>();
        speed = speed * -1;
        anim = GetComponent<Animator>();
    }
    void Update()
    {
        //Debug.Log(transform.right);
        transform.position += -transform.right * speed * Time.deltaTime;
    }

    private void FixedUpdate()
    {
        Vector2 startPos = transform.position + transform.right;
        Vector2 HitPos = transform.position + (transform.right * 1.2f);
        Debug.DrawLine(startPos, HitPos, Color.red);
        Debug.DrawLine(startPos, transform.position + (15*transform.right) , Color.green);
        Debug.DrawLine(startPos, transform.position + (15 * (transform.right + transform.up).normalized), Color.green);
        Debug.DrawLine(startPos, transform.position + (15 * (transform.right - transform.up).normalized), Color.green);
        Debug.DrawLine(startPos, startPos + (Vector2.down*2.5f), Color.red);
        //Debug.DrawLine(startPos, startPos + (Vector2.down * 2.5f), Color.red);
        RaycastHit2D hit1 = Physics2D.Raycast(startPos, Vector2.down, 3f, LayerMask.GetMask("Floor"));
        RaycastHit2D hitAttck1 = Physics2D.Raycast(startPos, transform.right, 15f, LayerMask.GetMask("Player"));
        RaycastHit2D hitAttck2 = Physics2D.Raycast(startPos, transform.right - transform.up, 15f, LayerMask.GetMask("Player"));
        RaycastHit2D hitAttck3 = Physics2D.Raycast(startPos, transform.right + transform.up, 15f, LayerMask.GetMask("Player"));
        RaycastHit2D hit3 = Physics2D.Raycast(startPos, transform.right, 1.2f, LayerMask.GetMask("Floor"));
        if (!hit1 || hit3)
        {
            transform.right = -transform.right;
        }
         
        if (hitAttck1)
        {
            if (Time.time - lastAttckTime < AttckCd) return; 
            else lastAttckTime = Time.time;
            anim.SetTrigger("Attck1");
            StartCoroutine(attck());
        }
        if(hitAttck2)
        {
            if (Time.time - lastAttckTime < AttckCd) return;
            else lastAttckTime = Time.time;
            anim.SetTrigger("Attck2");
            StartCoroutine(attck());
        }
        if(hitAttck3)
        {
            if (Time.time - lastAttckTime < AttckCd) return;
            else lastAttckTime = Time.time;
            anim.SetTrigger("Attck3");
            StartCoroutine(attck());
        }
    }

    IEnumerator attck()
    {
        for(int i=1;i<=3;i++)
        {
            Instantiate(bullet, shootpos.transform.position, Quaternion.identity);
            yield return new WaitForSeconds(0.3f);
        }
        
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("PlayerBullet"))
        {
            anim.SetTrigger("BeHurt");
            Debug.Log(HP);
            if (this.HP <= 0)
            {
                getGold(100);
                anim.SetTrigger("Die");
                Debug.Log("Die");
                gameObject.layer = LayerMask.NameToLayer("BG");
                
            }
        }
    }

}
